package com.benyanyi.okhttp.download;

import android.content.Context;

import com.benyanyi.okhttp.listener.OnDownLoadObserver;

/**
 * @author YanYi
 * @date 2019-08-15 10:23
 * @email ben@yanyi.red
 * @overview
 */
public class DownloadCall implements DownloadConfig {

    private Context mContext;
    private String url;
    private String suffix;
    private boolean isCover;

    private DownloadCall(Builder builder) {
        this.mContext = builder.mContext;
        this.url = builder.url;
        this.suffix = builder.suffix;
        this.isCover = builder.isCover;
    }

    @Override
    public void start() {
        DownloadManager.getInstance(mContext).download(url, isCover, suffix);
    }

    @Override
    public void start(OnDownLoadObserver onDownLoadObserver) {
        DownloadManager.getInstance(mContext).download(url, isCover, suffix, onDownLoadObserver);
    }

    @Override
    public void pause() {
        DownloadManager.getInstance(mContext).pause(url);
    }

    @Override
    public void cancel() {
        DownloadManager.getInstance(mContext).cancel(url);
    }

    @Override
    public boolean getDownloadUrl() {
        return DownloadManager.getInstance(mContext).getDownloadUrl(url);
    }

    @Override
    public DownloadInfo getDownloadInfo() {
        return DownloadManager.getInstance(mContext).getDownloadInfo(url);
    }

    public static class Builder {

        private Context mContext;
        private String url;
        private String suffix;
        private boolean isCover;

        public Builder setContext(Context mContext) {
            this.mContext = mContext;
            return this;
        }

        public Builder setUrl(String url) {
            this.url = url;
            return this;
        }

        public Builder setSuffix(String suffix) {
            this.suffix = suffix;
            return this;
        }

        public Builder setCover(boolean cover) {
            isCover = cover;
            return this;
        }

        public DownloadConfig builder() {
            return new DownloadCall(this);
        }

    }

}
